package com.javastudy.autowiredStudy.defineBean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author mac
 * @date 2021/9/10 10:24 上午
 */
public class StaticFactory {
    /**
     * 静态工厂方法
     * @param name
     * @param age
     * @return
     */
    public static Person getPerson(String name,int age){
        return new Person(name,age);
    }

    /**
     * 实例工厂
     * @param name
     * @param age
     * @return
     */
    public Person getPerson1(String name,int age){
        return new Person(name,age);
    }
}

@AllArgsConstructor
@NoArgsConstructor
@Data
class Person{
    private String name;
    private int age;
}

/**
 * 实现factorybean可以解决xml配置实例工厂的
 *  ---回顾：
 *      getBean("a")是获取的这个方法的getObject()返回的对象
 *      getBean("&a")是获取的实现了factoryBean接口的类的对象
 */
@Component
class PersonFactoryBean implements FactoryBean<Person>{
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}